<?php
/**
 * This class returns links to databases given an accession number
 */
class ParseAccessions {
    private $accessions = array();
    public function __construct() {
        $curLoc = __FILE__;
        $curLoc = str_replace("parseAccession.inc", "accessionMap.txt", $curLoc);
        $rawAccessions = file($curLoc);
        foreach($rawAccessions AS $line) {
            list($accessionFormat,$link) = split("\t",$line);
            $this->accessions[] = array($accessionFormat,$link);
        }
    }
    /**
     * Given a string (e.g. sentence or definition line,
     * this function returns all links for known accession number formats.
     * @param <type> $string
     * @return <type>
     */
    public function getLinks($string) {
        $links = array();
        $tmp = str_replace(">", " ", $string);
        $words = split(" ",$tmp);
        foreach($words AS $word) {
            foreach($accessions AS $accession) {
                $pattern = $accession[0];
                $link = $accession[1];
                if(preg_match($pattern, $word) > 0) {
                    $links[] = str_replace("%ACCNUM%", $word, $link);
                }
            }
        }
        return($links);
    }
}
?>
